package org.feng.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import org.feng.entity.pojo.Datasource;
import org.feng.pojo.Result;
import org.feng.service.DatasourceService;
import org.feng.utils.condition.Condition;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 数据源
 * </p>
 *
 * @author feng
 * @since 2022-02-23
 */
@RestController
@RequestMapping("/system/datasource")
@AllArgsConstructor
@Api(value = "数据源", tags = "数据源")
public class DatasourceController {

    private DatasourceService datasourceService;

    /**
     * 列表
     */
    @ApiOperation(value = "列表", notes = "全部数据")
    @GetMapping()
    public Result<List<Datasource>> list(Datasource datasource) {
        return Result.success(datasourceService.list(Condition.getQueryWrapper(datasource)));
    }

    /**
     * 分页列表
     */
    @ApiOperation(value = "分页列表", notes = "分页数据")
    @GetMapping("/page")
    public Result<IPage<Datasource>> page(Datasource datasource) {
        return Result.success(datasourceService.page(Condition.getPage(), Condition.getQueryWrapper(datasource)));
    }

    /**
     * 详情
     */
    @ApiOperation(value = "数据详情", notes = "传输id")
    @GetMapping("/query")
    public Result<Datasource> detail(@RequestParam Long id) {
        return Result.success(datasourceService.getById(id));
    }

    /**
     * 新增
     */
    @ApiOperation(value = "保存数据", notes = "传输对象")
    @PostMapping()
    public Result<Boolean> save(@RequestBody Datasource datasource) {
        return Result.status(datasourceService.save(datasource));
    }

    /**
     * 修改
     */
    @ApiOperation(value = "更新数据", notes = "传输对象")
    @PutMapping()
    public Result<Boolean> update(@RequestBody Datasource datasource) {
        return Result.status(datasourceService.updateById(datasource));
    }

    /**
     * 删除
     */
    @ApiOperation(value = "删除数据", notes = "传输id数组")
    @DeleteMapping()
    public Result<Boolean> delete(@RequestParam String ids) {
        return Result.status(datasourceService.removeByIds(Arrays.asList(ids.split(","))));
    }
}

